home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / wincapt.arj / COPY.C < prev    next >
Text File  |  1992-06-24  |  19KB  |  594 lines

  1. //**********************************************************************
  2. //
  3. //  copy.c
  4. //
  5. //  Source file for Device-Independent Bitmap (DIB) API.  Provides
  6. //  the following functions:
  7. //
  8. //  CopyWindowToDIB()   - Copies a window to a DIB
  9. //  CopyScreenToDIB()   - Copies entire screen to a DIB
  10. //  CopyWindowToBitmap()- Copies a window to a standard Bitmap
  11. //  CopyScreenToBitmap()- Copies entire screen to a standard Bitmap
  12. //  PaintDIB()          - Displays DIB in the specified DC
  13. //  PaintBitmap()       - Displays bitmap in the specified DC
  14. //
  15. //  The following functions are called from DIBUTIL.C:
  16. //
  17. //  DIBToBitmap()       - Creates a bitmap from a DIB
  18. //  BitmapToDIB()       - Creates a DIB from a bitmap
  19. //  DIBWidth()          - Gets the width of the DIB
  20. //  DIBHeight()         - Gets the height of the DIB
  21. //  CreateDIBPalette()  - Gets the DIB's palette
  22. //  GetSystemPalette()  - Gets the current palette
  23. //
  24. // Development Team: Mark Bader
  25. //                   Patrick Schreiber
  26. //                   Garrett McAuliffe
  27. //                   Eric Flo
  28. //                   Tony Claflin
  29. //
  30. // Written by Microsoft Product Support Services, Developer Support.
  31. // Copyright (c) 1991 Microsoft Corporation. All rights reserved.
  32. //**********************************************************************
  33.  
  34. /* header files */
  35. #include <WINDOWS.H>
  36. #include "DIBUTIL.H"
  37. #include "DIBAPI.H"
  38.  
  39. /*************************************************************************
  40.  *
  41.  * CopyWindowToDIB()
  42.  *
  43.  * Parameters:
  44.  *
  45.  * HWND hWnd        - specifies the window
  46.  *
  47.  * WORD fPrintArea  - specifies the window area to copy into the device-
  48.  *                    independent bitmap
  49.  *
  50.  * Return Value:
  51.  *
  52.  * HDIB             - identifies the device-independent bitmap
  53.  *
  54.  * Description:
  55.  *
  56.  * This function copies the specified part(s) of the window to a device-
  57.  * independent bitmap.
  58.  *
  59.  * History:   Date      Author              Reason
  60.  *            9/15/91   Patrick Schreiber   Created
  61.  *            9/25/91   Patrick Schreiber   Added header and comments
  62.  *
  63.  ************************************************************************/
  64.  
  65.  
  66. HDIB FAR CopyWindowToDIB(HWND hWnd, WORD fPrintArea)
  67. {
  68.    HDIB hDIB = NULL;  // handle to DIB
  69.  
  70.    /* check for a valid window handle */
  71.  
  72.    if (!hWnd)
  73.       return NULL;
  74.    switch (fPrintArea)
  75.       {
  76.    case PW_WINDOW: // copy entire window
  77.    {
  78.       RECT rectWnd;
  79.  
  80.       /* get the window rectangle */
  81.  
  82.       GetWindowRect(hWnd, &rectWnd);
  83.  
  84.       /*  get the DIB of the window by calling
  85.        *  CopyScreenToDIB and passing it the window rect
  86.        */
  87.       hDIB = CopyScreenToDIB(&rectWnd);
  88.    }
  89.       break;
  90.  
  91.    case PW_CLIENT: // copy client area
  92.    {
  93.       RECT rectClient;
  94.       POINT pt1, pt2;
  95.  
  96.       /* get the client area dimensions */
  97.  
  98.       GetClientRect(hWnd, &rectClient);
  99.  
  100.       /* convert client coords to screen coords */
  101.       pt1.x = rectClient.left;
  102.       pt1.y = rectClient.top;
  103.       pt2.x = rectClient.right;
  104.       pt2.y = rectClient.bottom;
  105.       ClientToScreen(hWnd, &pt1);
  106.       ClientToScreen(hWnd, &pt2);
  107.       rectClient.left = pt1.x;
  108.       rectClient.top = pt1.y;
  109.       rectClient.right = pt2.x;
  110.       rectClient.bottom = pt2.y;
  111.  
  112.       /*  get the DIB of the client area by calling
  113.        *  CopyScreenToDIB and passing it the client rect
  114.        */
  115.       hDIB = CopyScreenToDIB(&rectClient);
  116.    }
  117.       break;
  118.  
  119.    default:    // invalid print area
  120.       return NULL;
  121.       }
  122.  
  123.    /* return the handle to the DIB */
  124.    return hDIB;
  125. }
  126.  
  127.  
  128. /*************************************************************************
  129.  *
  130.  * CopyScreenToDIB()
  131.  *
  132.  * Parameter:
  133.  *
  134.  * LPRECT lpRect    - specifies the window
  135.  *
  136.  * Return Value:
  137.  *
  138.  * HDIB             - identifies the device-independent bitmap
  139.  *
  140.  * Description:
  141.  *
  142.  * This function copies the specified part of the screen to a device-
  143.  * independent bitmap.
  144.  *
  145.  * History:   Date      Author             Reason
  146.  *            9/15/91   Patrick Schreiber  Created
  147.  *            9/25/91   Patrick Schreiber  Added header and comments
  148.  *            12/10/91  Patrick Schreiber  Released palette
  149.  *
  150.  ************************************************************************/
  151.  
  152.  
  153. HDIB FAR CopyScreenToDIB(LPRECT lpRect)
  154. {
  155.    HBITMAP hBitmap;    // handle to device-dependent bitmap
  156.    HPALETTE hPalette;  // handle to palette
  157.    HDIB hDIB = NULL;   // handle to DIB
  158.  
  159.    /*  get the device-dependent bitmap in lpRect by calling
  160.     *  CopyScreenToBitmap and passing it the rectangle to grab
  161.     */
  162.  
  163.    hBitmap = CopyScreenToBitmap(lpRect);
  164.  
  165.    /* check for a valid bitmap handle */
  166.    if (!hBitmap)
  167.       return NULL;
  168.  
  169.    /* get the current palette */
  170.    hPalette = GetSystemPalette();
  171.  
  172.    /* convert the bitmap to a DIB */
  173.    hDIB = BitmapToDIB(hBitmap, hPalette);
  174.  
  175.    /* clean up */
  176.    DeleteObject(hPalette);
  177.    DeleteObject(hBitmap);
  178.  
  179.    /* return handle to the packed-DIB */
  180.    return hDIB;
  181. }
  182.  
  183.  
  184. /*************************************************************************
  185.  *
  186.  * CopyWindowToBitmap()
  187.  *
  188.  * Parameters:
  189.  *
  190.  * HWND hWnd        - specifies the window
  191.  *
  192.  * WORD fPrintArea  - specifies the window area to copy into the device-
  193.  *                    dependent bitmap
  194.  *
  195.  * Return Value:
  196.  *
  197.  * HDIB         - identifies the device-dependent bitmap
  198.  *
  199.  * Description:
  200.  *
  201.  * This function copies the specified part(s) of the window to a device-
  202.  * dependent bitmap.
  203.  *
  204.  * History:   Date      Author              Reason
  205.  *            9/15/91   Patrick Schreiber   Created
  206.  *            9/25/91   Patrick Schreiber   Added header and comments
  207.  *
  208.  ************************************************************************/
  209.  
  210.  
  211. HBITMAP FAR CopyWindowToBitmap(HWND hWnd, WORD fPrintArea)
  212. {
  213.    HBITMAP hBitmap = NULL;  // handle to device-dependent bitmap
  214.  
  215.    /* check for a valid window handle */
  216.  
  217.    if (!hWnd)
  218.       return NULL;
  219.    switch (fPrintArea)
  220.       {
  221.    case PW_WINDOW: // copy entire window
  222.    {
  223.       RECT rectWnd;
  224.  
  225.       /* get the window rectangle */
  226.  
  227.       GetWindowRect(hWnd, &rectWnd);
  228.  
  229.       /*  get the bitmap of that window by calling
  230.        *  CopyScreenToBitmap and passing it the window rect
  231.        */
  232.       hBitmap = CopyScreenToBitmap(&rectWnd);
  233.    }
  234.    break;
  235.  
  236.    case PW_CLIENT: // copy client area
  237.    {
  238.       RECT rectClient;
  239.       POINT pt1, pt2;
  240.  
  241.       /* get client dimensions */
  242.  
  243.       GetClientRect(hWnd, &rectClient);
  244.  
  245.       /* convert client coords to screen coords */
  246.       pt1.x = rectClient.left;
  247.       pt1.y = rectClient.top;
  248.       pt2.x = rectClient.right;
  249.       pt2.y = rectClient.bottom;
  250.       ClientToScreen(hWnd, &pt1);
  251.       ClientToScreen(hWnd, &pt2);
  252.       rectClient.left = pt1.x;
  253.       rectClient.top = pt1.y;
  254.       rectClient.right = pt2.x;
  255.       rectClient.bottom = pt2.y;
  256.  
  257.       /*  get the bitmap of the client area by calling
  258.        *  CopyScreenToBitmap and passing it the client rect
  259.        */
  260.       hBitmap = CopyScreenToBitmap(&rectClient);
  261.    }
  262.    break;
  263.  
  264.    default:    // invalid print area
  265.       return NULL;
  266.       }
  267.  
  268.    /* return handle to the bitmap */
  269.    return hBitmap;
  270. }
  271.  
  272.  
  273. /*************************************************************************
  274.  *
  275.  * CopyScreenToBitmap()
  276.  *
  277.  * Parameter:
  278.  *
  279.  * LPRECT lpRect    - specifies the window
  280.  *
  281.  * Return Value:
  282.  *
  283.  * HDIB             - identifies the device-dependent bitmap
  284.  *
  285.  * Description:
  286.  *
  287.  * This function copies the specified part of the screen to a device-
  288.  * dependent bitmap.
  289.  *
  290.  * History:   Date      Author             Reason
  291.  *            9/15/91   Patrick Schreiber  Created
  292.  *            9/25/91   Patrick Schreiber  Added header and comments
  293.  *
  294.  ************************************************************************/
  295.  
  296.  
  297. HBITMAP FAR CopyScreenToBitmap(LPRECT lpRect)
  298. {
  299.    HDC hScrDC, hMemDC;           // screen DC and memory DC
  300.    HBITMAP hBitmap, hOldBitmap;  // handles to deice-dependent bitmaps
  301.    int nX, nY, nX2, nY2;         // coordinates of rectangle to grab
  302.    int nWidth, nHeight;          // DIB width and height
  303.    int xScrn, yScrn;             // screen resolution
  304.  
  305.    /* check for an empty rectangle */
  306.  
  307.    if (IsRectEmpty(lpRect))
  308.       return NULL;
  309.  
  310.    /*  create a DC for the screen and create
  311.     *  a memory DC compatible to screen DC
  312.     */
  313.    hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  314.    hMemDC = CreateCompatibleDC(hScrDC);
  315.  
  316.    /* get points of rectangle to grab */
  317.    nX = lpRect->left;
  318.    nY = lpRect->top;
  319.    nX2 = lpRect->right;
  320.    nY2 = lpRect->bottom;
  321.  
  322.    /* get screen resolution */
  323.    xScrn = GetDeviceCaps(hScrDC, HORZRES);
  324.    yScrn = GetDeviceCaps(hScrDC, VERTRES);
  325.  
  326.    /* make sure bitmap rectangle is visible */
  327.    if (nX < 0)
  328.       nX = 0;
  329.    if (nY < 0)
  330.       nY = 0;
  331.    if (nX2 > xScrn)
  332.       nX2 = xScrn;
  333.    if (nY2 > yScrn)
  334.       nY2 = yScrn;
  335.    nWidth = nX2 - nX;
  336.    nHeight = nY2 - nY;
  337.  
  338.    /* create a bitmap compatible with the screen DC */
  339.    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
  340.  
  341.    /* select new bitmap into memory DC */
  342.    hOldBitmap = SelectObject(hMemDC, hBitmap);
  343.  
  344.    /* bitblt screen DC to memory DC */
  345.    BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
  346.  
  347.    /*  select old bitmap back into memory DC and get handle to
  348.     *  bitmap of the screen
  349.     */
  350.    hBitmap = SelectObject(hMemDC, hOldBitmap);
  351.  
  352.    /* clean up */
  353.    DeleteDC(hScrDC);
  354.    DeleteDC(hMemDC);
  355.  
  356.    /* return handle to the bitmap */
  357.    return hBitmap;
  358. }
  359.  
  360.  
  361. /*************************************************************************
  362.  *
  363.  * PaintDIB()
  364.  *
  365.  * Parameters:
  366.  *
  367.  * HDC hDC          - DC to do output to
  368.  *
  369.  * LPRECT lpDCRect  - rectangle on DC to do output to
  370.  *
  371.  * HDIB hDIB        - handle to global memory with a DIB spec
  372.  *                    in it followed by the DIB bits
  373.  *
  374.  * LPRECT lpDIBRect - rectangle of DIB to output into lpDCRect
  375.  *
  376.  * Return Value:
  377.  *
  378.  * BOOL             - TRUE if DIB was drawn, FALSE otherwise
  379.  *
  380.  * Description:
  381.  *   Painting routine for a DIB.  Calls StretchDIBits() or
  382.  *   SetDIBitsToDevice() to paint the DIB.  The DIB is
  383.  *   output to the specified DC, at the coordinates given
  384.  *   in lpDCRect.  The area of the DIB to be output is
  385.  *   given by lpDIBRect.
  386.  *
  387.  * NOTE: This function always selects the palette as background. Before
  388.  * calling this function, be sure your palette is selected to desired
  389.  * priority (foreground or background).
  390.  *
  391.  * History:   
  392.  *            
  393.  *    Date      Author               Reason        
  394.  *    6/1/91    Garrett McAuliffe    Created        
  395.  *    12/12/91  Patrick Schreiber    Removed palette param, added get        
  396.  *                                   palette stuff, return value, header
  397.  *                                   and some comments
  398.  *    6/8/92    Patrick Schreiber    Put palette param back in, select as
  399.  *                                   background palette always. Added NOTE
  400.  *                                   above.
  401.  *
  402.  ************************************************************************/
  403.  
  404. BOOL FAR PaintDIB(HDC      hDC,
  405.                   LPRECT   lpDCRect,
  406.                   HDIB     hDIB,
  407.                   LPRECT   lpDIBRect,
  408.                   HPALETTE hPal)
  409. {
  410.    LPSTR    lpDIBHdr;            // Pointer to BITMAPINFOHEADER
  411.    LPSTR    lpDIBBits;           // Pointer to DIB bits
  412.    BOOL     bSuccess=FALSE;      // Success/fail flag
  413.    HPALETTE hOldPal=NULL;        // Previous palette
  414.  
  415.    /* Check for valid DIB handle */
  416.    if (!hDIB)
  417.       return FALSE;
  418.  
  419.    /* Lock down the DIB, and get a pointer to the beginning of the bit
  420.     *  buffer
  421.     */
  422.    lpDIBHdr  = GlobalLock(hDIB);
  423.    lpDIBBits = FindDIBBits(lpDIBHdr);
  424.  
  425.    /* Select and realize our palette as background */
  426.    if (hPal)
  427.    {
  428.       hOldPal = SelectPalette(hDC, hPal, TRUE);
  429.       RealizePalette(hDC);
  430.    }
  431.  
  432.    /* Make sure to use the stretching mode best for color pictures */
  433.    SetStretchBltMode(hDC, COLORONCOLOR);
  434.  
  435.    /* Determine whether to call StretchDIBits() or SetDIBitsToDevice() */
  436.    if ((RECTWIDTH(lpDCRect)  == RECTWIDTH(lpDIBRect)) &&
  437.        (RECTHEIGHT(lpDCRect) == RECTHEIGHT(lpDIBRect)))
  438.       bSuccess = SetDIBitsToDevice(hDC,                        // hDC
  439.                                    lpDCRect->left,             // DestX
  440.                                    lpDCRect->top,              // DestY
  441.                                    RECTWIDTH(lpDCRect),        // nDestWidth
  442.                                    RECTHEIGHT(lpDCRect),       // nDestHeight
  443.                                    lpDIBRect->left,            // SrcX
  444.                                    (int)DIBHeight(lpDIBHdr) -
  445.                                       lpDIBRect->top - 
  446.                                       RECTHEIGHT(lpDIBRect),   // SrcY
  447.                                    0,                          // nStartScan
  448.                                    (WORD)DIBHeight(lpDIBHdr),  // nNumScans
  449.                                    lpDIBBits,                  // lpBits
  450.                                    (LPBITMAPINFO)lpDIBHdr,     // lpBitsInfo
  451.                                    DIB_RGB_COLORS);            // wUsage
  452.    else 
  453.       bSuccess = StretchDIBits(hDC,                            // hDC
  454.                                lpDCRect->left,                 // DestX
  455.                                lpDCRect->top,                  // DestY
  456.                                RECTWIDTH(lpDCRect),            // nDestWidth
  457.                                RECTHEIGHT(lpDCRect),           // nDestHeight
  458.                                lpDIBRect->left,                // SrcX
  459.                                lpDIBRect->top,                 // SrcY
  460.                                RECTWIDTH(lpDIBRect),           // wSrcWidth
  461.                                RECTHEIGHT(lpDIBRect),          // wSrcHeight
  462.                                lpDIBBits,                      // lpBits
  463.                                (LPBITMAPINFO)lpDIBHdr,         // lpBitsInfo
  464.                                DIB_RGB_COLORS,                 // wUsage
  465.                                SRCCOPY);                       // dwROP
  466.  
  467.    /* Unlock the memory block */
  468.    GlobalUnlock(hDIB);
  469.  
  470.    /* Reselect old palette */
  471.    if (hOldPal)
  472.      SelectPalette(hDC, hOldPal, FALSE);
  473.  
  474.    /* Return with success/fail flag */
  475.    return bSuccess;
  476. }
  477.  
  478.  
  479. /*************************************************************************
  480.  *
  481.  * PaintBitmap()
  482.  *
  483.  * Parameters:
  484.  *
  485.  * HDC hDC          - DC to do output to
  486.  *
  487.  * LPRECT lpDCRect  - rectangle on DC to do output to
  488.  *
  489.  * HBITMAP hDDB     - handle to device-dependent bitmap (DDB)
  490.  *
  491.  * LPRECT lpDDBRect - rectangle of DDB to output into lpDCRect
  492.  *
  493.  * HPALETTE hPalette - handle to the palette to use with hDDB
  494.  *
  495.  * Return Value:
  496.  *
  497.  * BOOL             - TRUE if bitmap was drawn, FLASE otherwise
  498.  *
  499.  * Description:
  500.  *
  501.  * Painting routine for a DDB.  Calls BitBlt() or
  502.  * StretchBlt() to paint the DDB.  The DDB is
  503.  * output to the specified DC, at the coordinates given
  504.  * in lpDCRect.  The area of the DDB to be output is
  505.  * given by lpDDBRect.  The specified palette is used.
  506.  *
  507.  * NOTE: This function always selects the palette as background. Before
  508.  * calling this function, be sure your palette is selected to desired
  509.  * priority (foreground or background).
  510.  *
  511.  * History:   
  512.  *            
  513.  *   Date      Author               Reason         
  514.  *   6/1/91    Garrett McAuliffe    Created         
  515.  *   12/12/91  Patrick Schreiber    Added return value, realizepalette,
  516.  *                                       header and some comments
  517.  *   6/8/92    Patrick Schreiber    Select palette as background always, added
  518.  *                                  NOTE above.
  519.  *
  520.  ************************************************************************/
  521.  
  522. BOOL FAR PaintBitmap(HDC      hDC,
  523.                      LPRECT   lpDCRect, 
  524.                      HBITMAP  hDDB, 
  525.                      LPRECT   lpDDBRect, 
  526.                      HPALETTE hPal)
  527. {
  528.    HDC      hMemDC;            // Handle to memory DC
  529.    HBITMAP  hOldBitmap;        // Handle to previous bitmap
  530.    HPALETTE hOldPal1 = NULL;   // Handle to previous palette
  531.    HPALETTE hOldPal2 = NULL;   // Handle to previous palette
  532.    BOOL     bSuccess = FALSE;  // Success/fail flag
  533.  
  534.    /* Create a memory DC */
  535.    hMemDC = CreateCompatibleDC (hDC);
  536.  
  537.    /* If this failed, return FALSE */
  538.    if (!hMemDC)
  539.       return FALSE;
  540.  
  541.    /* If we have a palette, select and realize it */
  542.    if (hPal)
  543.    {
  544.       hOldPal1 = SelectPalette(hMemDC, hPal, TRUE);
  545.       hOldPal2 = SelectPalette(hDC, hPal, TRUE);
  546.       RealizePalette(hDC);
  547.    }
  548.  
  549.    /* Select bitmap into the memory DC */
  550.    hOldBitmap = SelectObject (hMemDC, hDDB);
  551.  
  552.    /* Make sure to use the stretching mode best for color pictures */   
  553.    SetStretchBltMode (hDC, COLORONCOLOR);
  554.  
  555.    /* Determine whether to call StretchBlt() or BitBlt() */ 
  556.    if ((RECTWIDTH(lpDCRect)  == RECTWIDTH(lpDDBRect)) &&
  557.        (RECTHEIGHT(lpDCRect) == RECTHEIGHT(lpDDBRect)))
  558.       bSuccess = BitBlt(hDC,
  559.                         lpDCRect->left,
  560.                         lpDCRect->top,
  561.                         lpDCRect->right - lpDCRect->left,
  562.                         lpDCRect->bottom - lpDCRect->top,
  563.                         hMemDC,
  564.                         lpDDBRect->left,
  565.                         lpDDBRect->top,
  566.                         SRCCOPY);
  567.    else
  568.       bSuccess = StretchBlt(hDC,
  569.                             lpDCRect->left, 
  570.                             lpDCRect->top, 
  571.                             lpDCRect->right - lpDCRect->left,
  572.                             lpDCRect->bottom - lpDCRect->top,
  573.                             hMemDC,
  574.                             lpDDBRect->left, 
  575.                             lpDDBRect->top, 
  576.                             lpDDBRect->right - lpDDBRect->left,
  577.                             lpDDBRect->bottom - lpDDBRect->top,
  578.                             SRCCOPY);
  579.  
  580.    /* Clean up */
  581.    SelectObject(hMemDC, hOldBitmap);
  582.  
  583.    if (hOldPal1)
  584.       SelectPalette (hMemDC, hOldPal1, FALSE);
  585.  
  586.    if (hOldPal2)
  587.       SelectPalette (hDC, hOldPal2, FALSE);
  588.  
  589.    DeleteDC (hMemDC);
  590.  
  591.    /* Return with success/fail flag */
  592.    return bSuccess;
  593. }
  594.